package com.CTAtracker.android.DataBase;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

/**
 * This class is an helper for the connection to the database for the table line
 */
public class MySQLiteHelperLine extends MySQLiteHelper {

	/**
	 * The name of the table line
	 */
	public static final String TABLE_LINE = "lines";
	/**
	 * The name of the database field : idLine
	 */
	public static final String LINE_ID = "_idLine";
	/**
	 * The name of the database field : nameLine
	 */
	public static final String LINE_NAME = "nameLine";
	/**
	 * The name of the database field : colorLine
	 */
	public static final String LINE_COLOR = "colorLine";
	/**
	 * A string array of all the fields in the table
	 */
	public static final String[] allColumns = { 
			MySQLiteHelperLine.LINE_ID,
			MySQLiteHelperLine.LINE_NAME, 
			MySQLiteHelperLine.LINE_COLOR};
	/**
	 * The query to create the table line in the database
	 */
	private static final String DATABASE_CREATE_LINE = "create table " + TABLE_LINE 
			+ " (" + LINE_ID + " integer primary key autoincrement, " 
			+ LINE_NAME + " text not null, " 
			+ LINE_COLOR + " text not null);";

	/**
	 * Constructor, Instantiate the database with the method super
	 * @param context
	 */
	public MySQLiteHelperLine(Context context) {
		super(context);
	}

	/**
	 * This method is called the first time we create the database
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		MySQLiteHelperLine.createData(db); 
		MySQLiteHelperStation.createData(db);
		MySQLiteHelperFavorite.createData(db);
	}
	
	/**
	 * Create the query to insert a new line in the database
	 * @param database the database 
	 * @param name the name of the line
	 * @param color the color of the line 
	 * @throws SQLiteException
	 */
	public static void createLine(SQLiteDatabase database, String name, String color) throws SQLiteException {
		ContentValues values = new ContentValues();

		values.put(MySQLiteHelperLine.LINE_NAME, name);
		values.put(MySQLiteHelperLine.LINE_COLOR, color);

		if (database.insert(TABLE_LINE, null, values) == -1)
			throw new SQLiteException();
	}

	/**
	 * This method is called by the method onCreate. Create the table line and insert the data in the table
	 * @param db the database
	 */
	public static void createData(SQLiteDatabase db) {
		db.execSQL(DATABASE_CREATE_LINE);
		createLine(db, "Red","RED");
		createLine(db, "Blue","BLUE");
		createLine(db, "Green","GREEN");
		createLine(db, "Brown","BROWN");
		createLine(db, "Orange","ORANGE");
		createLine(db, "Pink","PINK");
		createLine(db, "Purple","PURPLE");
		createLine(db, "Yellow","YELLOW");
		createLine(db, "Congress","CONGRESS");
	}
}
